package com.hmall.common.config;

import cn.hutool.core.collection.CollUtil;
import com.hmall.common.interceptor.UserInfoInterceptor;
import com.hmall.geteway.config.AuthProperties;

import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@RequiredArgsConstructor
@EnableConfigurationProperties(AuthProperties.class)
@Configuration
public class MvConfig implements WebMvcConfigurer {
    private final AuthProperties authProperties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
       //1.添加拦截器
        UserInfoInterceptor loginInterceptor=new UserInfoInterceptor();
        InterceptorRegistration registration=registry.addInterceptor(loginInterceptor);
        //2.配置拦截路径
        List<String>includePaths=authProperties.getIncludePaths();
        if(CollUtil.isNotEmpty(includePaths)){
            registration.addPathPatterns(includePaths);
        }
        //3.配置放行路径
        List<String> excludePaths = authProperties.getExcludePaths();
        if(CollUtil.isNotEmpty(excludePaths)){
            registration.excludePathPatterns(excludePaths);
        }
        registration.excludePathPatterns(
                "/error",
                "/favicon.ico",
                "/v2/**",
                "/v3/**",
                "/swagger-resources/**",
                "/doc.html"
        );
    }
}
